﻿<Window x:Class="UIAutoXMLBuilder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UIAutoXMLBuilder"
        xmlns:mode="clr-namespace:UIEngine;assembly=UIEngine"
        Name="mainWin"
        Title="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Height="768" Width="1024" Icon="/Polaris;component/images/logo.ico" Closed="mainWin_Closed">
    <Window.Resources>
        <RoutedCommand x:Key="Exit"/>
        <RoutedCommand x:Key="Locate"/>
        <RoutedCommand x:Key="Operate"/>
        <RoutedCommand x:Key="Detect"/>
        <local:IngStatusConverter x:Key="IngStatusConverter" />
        <local:InvalidElementConverter x:Key="InvalidElementConverter" />
        <Style TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type UIElement}, AncestorLevel=1}, Path=IsEnabled}"  Value="False">
                    <Setter Property="Opacity"  Value="0.2" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <ControlTemplate x:Key="loadingAnimation">
            <ControlTemplate.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard Name="animation">
                            <Storyboard>
                                <DoubleAnimation From="0" To="359" Duration="0:0:3" RepeatBehavior="Forever"
                                                 Storyboard.TargetName="angle" Storyboard.TargetProperty="Angle"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="animation"/>
                    </Trigger.ExitActions>
                </Trigger>
            </ControlTemplate.Triggers>
            <Image Name="content" Opacity="1">
                <Image.Source>
                    <DrawingImage>
                        <DrawingImage.Drawing>
                            <DrawingGroup>
                                <GeometryDrawing Brush="Transparent">
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,1,1"/>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <DrawingGroup>
                                    <DrawingGroup.Transform>
                                        <RotateTransform x:Name="angle" Angle="0" CenterX="0.5" CenterY="0.5"/>
                                    </DrawingGroup.Transform>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Pen>
                                            <Pen Brush="Blue" Thickness="0.1"/>
                                        </GeometryDrawing.Pen>
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry>
                                                <PathFigure StartPoint="0.9,0.5">
                                                    <ArcSegment Point="0.5,0.1" RotationAngle="90" SweepDirection="Clockwise" IsLargeArc="True" Size="0.4,0.4"/>
                                                </PathFigure>
                                            </PathGeometry>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                    <GeometryDrawing Brush="Blue">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry>
                                                <PathFigure StartPoint="0.5,0">
                                                    <LineSegment Point="0.7,0.1" />
                                                    <LineSegment Point="0.5,0.2" />
                                                </PathFigure>
                                            </PathGeometry>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingGroup>
                            </DrawingGroup>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Image.Source>
            </Image>
        </ControlTemplate>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="Open" CanExecute="OpenCanExecute"  Executed="OpenExecute"/>
        <CommandBinding Command="Save" CanExecute="SaveCanExecute"  Executed="SaveExecute"/>
        <CommandBinding Command="SaveAs" CanExecute="SaveAsCanExecute"  Executed="SaveAsExecute"/>
        <CommandBinding Command="Close" CanExecute="CloseCanExecute"  Executed="CloseExecute"/>
        <CommandBinding Command="{StaticResource Exit}" CanExecute="ExitCanExecute"  Executed="ExitExecute"/>
        <CommandBinding Command="{StaticResource Detect}" CanExecute="DetectCanExecute"  Executed="DetectExecute"/>
        <CommandBinding Command="{StaticResource Locate}" CanExecute="LocateCanExecute"  Executed="LocateExecute"/>
      </Window.CommandBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="26"></RowDefinition>
            <RowDefinition Height="35"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Menu Name="menuMain" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="26">
            <MenuItem Header="_File">
                <MenuItem Header="_Open..." Command="Open">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="/Polaris;component/images/open.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Save" Command="Save">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="/Polaris;component/images/save.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_SaveAs..." Command="SaveAs">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="/Polaris;component/images/saveas.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Close" Command="Close">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="/Polaris;component/images/close.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="_Exit" Command="{StaticResource Exit}">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="/Polaris;component/images/exit.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="_Control">
                <MenuItem Header="_XML Control">
                    <MenuItem Header="_Add" Command="{Binding AddControlCommand}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="/Polaris;component/images/add.png" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="_Delete" Command="{Binding DeleteControlCommand}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="/Polaris;component/images/delete.png" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="_Locate" Command="{StaticResource Locate}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="/Polaris;component/images/locate.png" />
                        </MenuItem.Icon>
                    </MenuItem>
                </MenuItem>
                <MenuItem Header="_Detect Control">
                    <MenuItem Header="_Detect" Command="{StaticResource Detect}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="/Polaris;component/images/detect.png" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="_Bulk Add" Command="{Binding ExportControlsCommand}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="/Polaris;component/images/export.png" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="_Insert">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="/Polaris;component/images/insert.png" />
                        </MenuItem.Icon>
                        <MenuItem Header="_Insert Into" Command="{Binding InsertIntoControlCommand}">
                            <MenuItem.Icon>
                                <Image Width="16" Height="16" Source="/Polaris;component/images/insertinto.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="_Insert Before" Command="{Binding InsertBeforeControlCommand}">
                            <MenuItem.Icon>
                                <Image Width="16" Height="16" Source="/Polaris;component/images/insertbefore.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="_Insert After" Command="{Binding InsertAfterControlCommand}">
                            <MenuItem.Icon>
                                <Image Width="16" Height="16" Source="/Polaris;component/images/insertafter.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="_Replace" Command="{Binding ReplaceControlCommand}">
                            <MenuItem.Icon>
                                <Image Width="16" Height="16" Source="/Polaris;component/images/replace.png" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </MenuItem>
                </MenuItem>
            </MenuItem>
        </Menu>
        <ToolBar Height="34" HorizontalAlignment="Stretch" Name="mainToolBar" VerticalAlignment="Top" Grid.Row="1">
            <Button Height="32" HorizontalAlignment="Left" Name="btnOpen" VerticalAlignment="Top" Command="Open">
                <Image Source="/Polaris;component/images/open.png" ToolTip="Open" />
            </Button>
            <Button Height="32" HorizontalAlignment="Left" Name="btnSave" VerticalAlignment="Top" Command="Save">
                <Image Source="/Polaris;component/images/save.png" ToolTip="Save" />
            </Button>
            <Button Height="32" HorizontalAlignment="Left" Name="btnSaveAs" VerticalAlignment="Top" Command="SaveAs">
                <Image Source="/Polaris;component/images/saveas.png" ToolTip="SaveAs" />
            </Button>
            <Button Height="32" HorizontalAlignment="Left" Name="btnClose" VerticalAlignment="Top" Command="Close">
                <Image Source="/Polaris;component/images/close.png"  ToolTip="Close"/>
            </Button>
            <Separator></Separator>
            <Button Height="32" HorizontalAlignment="Left" Name="btnDetect" VerticalAlignment="Top" Command="{StaticResource Detect}">
                <Image Source="/Polaris;component/images/detect.png"  ToolTip="Detect"/>
            </Button>
            <Button Height="32" HorizontalAlignment="Left" Name="btnExport" VerticalAlignment="Top" Command="{Binding  ExportControlsCommand}">
                <Image Source="/Polaris;component/images/export.png" ToolTip="Bulk Add" />
            </Button>
            <ToggleButton Name="btnInsert" Click="btnInsert_Click" ToolTip="Insert" VerticalAlignment="Top">
                <StackPanel Orientation="Horizontal">
                    <Image Height="32" Source="/Polaris;component/images/insert.png" />
                    <TextBlock Text="Insert" Margin="2" VerticalAlignment="Center"></TextBlock>
                    <Path x:Name="BtnArrow" Margin="4" VerticalAlignment="Center" Width="6" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
                </StackPanel>
            </ToggleButton>
            <Popup x:Name="popupInsert" PlacementTarget="{Binding ElementName=btnInsert}">
                <Border>
                    <StackPanel>
                        <Button ToolTip="Insert Into" Command="{Binding  InsertIntoControlCommand}">
                            <StackPanel Orientation="Horizontal" Width="120">
                                <Image Height="32" Source="/Polaris;component/images/insertinto.png" />
                                <TextBlock Margin="2" VerticalAlignment="Center">Insert Into</TextBlock>
                            </StackPanel>
                        </Button>
                        <Button ToolTip="Insert Before" Command="{Binding  InsertBeforeControlCommand}">
                            <StackPanel Orientation="Horizontal" Width="120">
                                <Image Height="32" Source="/Polaris;component/images/insertbefore.png" />
                                <TextBlock Margin="2" VerticalAlignment="Center">Insert Before</TextBlock>
                            </StackPanel>
                        </Button>
                        <Button ToolTip="Insert After" Command="{Binding  InsertAfterControlCommand}">
                            <StackPanel Orientation="Horizontal" Width="120">
                                <Image Height="32" Source="/Polaris;component/images/insertafter.png" />
                                <TextBlock Margin="2" VerticalAlignment="Center">Insert After</TextBlock>
                            </StackPanel>
                        </Button>
                        <Button ToolTip="Replace" Command="{Binding  ReplaceControlCommand}">
                            <StackPanel Orientation="Horizontal" Width="120">
                                <Image Height="32" Source="/Polaris;component/images/replace.png" />
                                <TextBlock Margin="2" VerticalAlignment="Center">Replace</TextBlock>
                            </StackPanel>
                        </Button>
                    </StackPanel>
                </Border>
            </Popup>
            <Separator></Separator>
            <Button Height="32" HorizontalAlignment="Left" Name="btnAdd" VerticalAlignment="Top" Command="{Binding  AddControlCommand}">
                <Image Source="/Polaris;component/images/add.png" ToolTip="Add" />
            </Button>
            <Button Height="32" HorizontalAlignment="Left" Name="btnDelete" VerticalAlignment="Top" Command="{Binding  DeleteControlCommand}">
                <Image Source="/Polaris;component/images/delete.png" ToolTip="Delete" />
            </Button>
            <Button Height="32" HorizontalAlignment="Left" Name="btnLocate" VerticalAlignment="Top" Command="{StaticResource Locate}">
                <Image Source="/Polaris;component/images/locate.png"  ToolTip="Locate"/>
            </Button>            
        </ToolBar>
        <Grid Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" MinWidth="300" MaxWidth="500" ></ColumnDefinition>
                <ColumnDefinition Width="3"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="3"></ColumnDefinition>
                <ColumnDefinition Width="Auto" MaxWidth="350"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TabControl Grid.Column="0" Name="tabControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TabStripPlacement="Left" 
                        HorizontalContentAlignment="Left" SelectionChanged="TabControl_SelectionChanged"
                        SelectedIndex="{Binding SelectedTab, UpdateSourceTrigger=PropertyChanged}">
                <TabControl.Resources>
                    <Style TargetType="{x:Type TabItem}">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="Padding" Value="1" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="TabItem">
                                    <Border Name="Border" BorderThickness="1,1,1,0" BorderBrush="Gainsboro" CornerRadius="5,0,0,5" Margin="1,0" Padding="3">
                                        <ContentPresenter x:Name="ContentSite"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        ContentSource="Header"
                                        Margin="3,1">
                                            <ContentPresenter.LayoutTransform>
                                                <RotateTransform Angle="270" />
                                            </ContentPresenter.LayoutTransform>
                                        </ContentPresenter>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter TargetName="Border" Property="Background" Value="LightSkyBlue" />
                                        </Trigger>
                                        <Trigger Property="IsSelected" Value="False">
                                            <Setter TargetName="Border" Property="Background" Value="GhostWhite" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.Resources>
                <TabItem HorizontalAlignment="Stretch" Name="tabXMLControl" Height="Auto" VerticalAlignment="Stretch" Header="XML Control">
                    <TreeView Name="tvXMLControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  SelectedItemChanged="tvXMLControl_SelectedItemChanged"
                              ItemsSource="{Binding XMLControlCollection}">
                        <TreeView.ItemContainerStyle>
                            <Style TargetType="TreeViewItem">
                                <Setter Property="HorizontalContentAlignment" Value="Left" />
                                <Setter Property="VerticalContentAlignment" Value="Center" />
                                <Setter Property="IsSelected" Value="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" />
                                <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, UpdateSourceTrigger=PropertyChanged}" />
                            </Style>
                        </TreeView.ItemContainerStyle>
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type mode:UIControl}" ItemsSource="{Binding ChildControls}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Grid.Column="1" Text="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged}" 
                                           ToolTip="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged}"
                                           Foreground="{Binding Path=IsInvalid, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InvalidElementConverter}}"
                                               VerticalAlignment="Center"/>
                                    <ContentPresenter Content="{Binding Path=IsLocating, Converter={StaticResource IngStatusConverter}}" Margin="2" VerticalAlignment="Center" />
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>
                </TabItem>
                <TabItem HorizontalAlignment="Stretch" Name="tabDetectControl" Height="Auto" VerticalAlignment="Stretch" Header="Detected Control">
                    <TreeView Name="tvDetectControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                              SelectedItemChanged="tvDetectControl_SelectedItemChanged"
                              ItemsSource="{Binding UIControlCollection}">  
                        <TreeView.ItemContainerStyle>
                            <Style TargetType="TreeViewItem">
                                <Setter Property="HorizontalContentAlignment" Value="Left" />
                                <Setter Property="VerticalContentAlignment" Value="Center" />
                                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
                                <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, UpdateSourceTrigger=PropertyChanged}" />
                            </Style>
                        </TreeView.ItemContainerStyle>
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type mode:UIControl}" ItemsSource="{Binding ChildControls}">
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox Name="ckbExport" IsChecked="{Binding Path=IsExport, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
                                    <TextBlock Grid.Column="1" Text="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged}" />
                                    <ContentPresenter Content="{Binding Path=IsDetecting, Converter={StaticResource IngStatusConverter}}" Margin="2" />
                                </StackPanel>                                
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>                    
                </TabItem>
            </TabControl>
            <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  ShowsPreview="True"  Width="2" />
            <StackPanel Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Orientation="Vertical">
                <GroupBox HorizontalAlignment="Stretch" Name="gpDefinitions" Height="350" VerticalAlignment="Stretch" Header="Control Definition" >
                    <local:ControlDefinitionUserControl x:Name="controlDefinition" />
                </GroupBox>
                <GroupBox HorizontalAlignment="Stretch" Name="gpOperation" VerticalAlignment="Stretch" Header="Control Operation" Visibility="Collapsed" >
                    
                </GroupBox>
            </StackPanel>
            <GridSplitter Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  ShowsPreview="True"  Width="2" />
            <GroupBox Grid.Column="4" HorizontalAlignment="Stretch" Name="gpProperties" 
                      VerticalAlignment="Stretch" Header="Available Properties" Visibility="{Binding NativePropertiesVisibility, UpdateSourceTrigger=PropertyChanged}" >
                <ScrollViewer Name="scrollViewer" ScrollViewer.IsDeferredScrollingEnabled="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="2">
                    <Grid>
                        <DataGrid Name="gridControl" 
                                AutoGenerateColumns="False" 
                                GridLinesVisibility="All"  
                                VerticalAlignment="Stretch" 
                                HorizontalAlignment="Stretch" 
                                PreviewMouseWheel="gridControl_PreviewMouseWheel"
                                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                CanUserAddRows="False"
                                CanUserDeleteRows="False" 
                                CanUserSortColumns="False"
                                MouseDoubleClick="gridControl_MouseDoubleClick">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Name" FontWeight="Bold" Binding="{Binding Key}" IsReadOnly="True"></DataGridTextColumn>
                                <DataGridTextColumn Header="Value" Binding="{Binding Value}" IsReadOnly="True"></DataGridTextColumn>
                            </DataGrid.Columns>
                        </DataGrid>
                    </Grid>
                </ScrollViewer>
            </GroupBox>
        </Grid>
    </Grid>
</Window>
